package com.wbs.rest.resource;

import com.wbs.rest.config.SqlRestContext;
import com.wbs.rest.executor.SqlExecutor;
import com.wbs.rest.handler.SqlModelHandler;
import com.wbs.rest.model.MultiSqlModel;
import com.wbs.rest.model.SqlModel;
import com.wbs.rest.model.SqlModelEnum;
import org.springframework.http.HttpMethod;

import java.util.HashSet;
import java.util.Set;

public class SqlResourceInformation {

    private SqlModel sqlModel;

    private SqlModelEnum sqlModelEnum;

    private SqlRestContext sqlRestContext;

    public SqlResourceInformation(SqlModel sqlModel, SqlRestContext sqlRestContext) {
        this.sqlModel = sqlModel;
        this.sqlRestContext = sqlRestContext;
    }

    public SqlResourceInformation(SqlModel sqlModel, SqlRestContext sqlRestContext, SqlModelEnum sqlModelEnum) {
        this.sqlModel = sqlModel;
        this.sqlRestContext = sqlRestContext;
        this.sqlModelEnum = sqlModelEnum;
    }

    public void verifySupportedMethod(HttpMethod httpMethod, String resourceType) throws Exception {

        if(!sqlModel.getHttpMethods().contains(httpMethod)){
            throw new Exception("http method " + httpMethod.name() + " not match");
        }

        if(sqlModelEnum == SqlModelEnum.MULTI){
            MultiSqlModel multiSqlModel = (MultiSqlModel)this.sqlModel;
            for(SqlModel model : multiSqlModel.getSqlModels()){
                if(!model.getHttpMethods().contains(httpMethod)){
                    throw new Exception("http method not match");
                }
            }
        }

        if(!resourceType.equalsIgnoreCase(sqlModel.getSqlType())){
            throw new Exception("resource type do not match sql type");
        }

    }

    public Set<HttpMethod> getSupportedMethod(){
        Set<HttpMethod> httpMethods = new HashSet<>();
        httpMethods.add(HttpMethod.GET);
        return httpMethods;
    }

    public SqlModel getSqlModel() {
        return sqlModel;
    }

    public SqlExecutor getExecutor() {
        return sqlRestContext.getSqlExecutor();
    }

    public SqlModelEnum getSqlModelEnum() {
        return sqlModelEnum;
    }
}
